package com.test.hosp.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.common.result.Result;
import com.test.common.utils.MD5;
import com.test.hosp.service.HospitalSetService;
import com.test.model.hosp.HospitalSet;
import com.test.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

/**
 * @date 2021/7/27 -14:59
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin //允许跨域访问
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查看医院设置表的所有信息
     * @return
     */
    @ApiOperation("获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 通过数据删除
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除医院设置信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag)
            return Result.ok();
        return Result.fail();
    }

    @ApiOperation("分页查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current, limit);
//        构造条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(hospitalSetQueryVo != null){
            String hoscode = hospitalSetQueryVo.getHoscode();//编号
            String hosname = hospitalSetQueryVo.getHosname();//名称
            if(!StringUtils.isEmpty(hosname)){
                wrapper.like("hosname",hospitalSetQueryVo.getHosname());
            }
            if(!StringUtils.isEmpty(hoscode)){
                wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
            }
        }

        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return Result.ok(pageHospitalSet);
    }

    @ApiOperation("添加医院设置信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 0 为不能使用 ，1 为可以使用
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag)
            return Result.ok();
        return Result.fail();
    }

    @ApiOperation("根据id获取医院设置信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation("根据id修改医院设置信息")
    @PostMapping("updateHospSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag)
            return Result.ok();
        return Result.fail();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batch")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if(flag)
            return Result.ok();
        return Result.fail();
    }

    @ApiOperation("锁定")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag)
            return Result.ok();
        return Result.fail();
    }

    @ApiOperation("发送签名")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //发送短信
        //。。。。。
        return Result.ok();
    }
}
